//
//  HMHomeOriginalView.swift
//  WB25
//
//  Created by HM on 2017/7/4.
//  Copyright © 2017年 itheima. All rights reserved.
//

import UIKit
import SnapKit
import YYText
class HMHomeOriginalView: UIView {
    
    // 记录原创微博的底部约束
    var originalViewBottomConstraint: Constraint?
    // 01-闭包声明
    var closure:(()->())?
    
    // 提供一个模型属性 供外界赋值
    var statusViewModel: HMStatusViewModel?{
        didSet{
            // 赋值
            headImageView.hm_setImage(withUrlString: statusViewModel?.homeModel?.user?.profile_image_url, placeholderImgName: "avatar_default_big")
            nameLabel.text = statusViewModel?.homeModel?.user?.name
            verifiedImageView.image = statusViewModel?.verifiedImage
            mbrankImageView.image = statusViewModel?.mbrankImage
            contentLabel.attributedText = statusViewModel?.originalAttributedText
            /*
                - 卸载riginalView.bottom
                - 判断是否有配图
                    - 判断条件statusViewModel?.homeModel?.pic_urls?.count > 0
                        - 如果count > 0 有配图
                            - 赋值
                            - originalView.bottom = pictureView.bottom + 10
                            - 显示配图
                        - 反之 没有配图
                            - originalView.bottom = contentLabel.bottom + 10
                            - 隐藏配图
             
             */
            // 卸载约束
            originalViewBottomConstraint?.deactivate()
            // 判断是否有配图
            // 如果有
            if let picUrls = statusViewModel?.homeModel?.pic_urls, picUrls.count > 0 {
                
                // 赋值
                pictureView.picUrlsList = picUrls
                
                snp.makeConstraints({ (make) in
                    originalViewBottomConstraint = make.bottom.equalTo(pictureView).offset(10).constraint
                })
                
                // 显示配图
                pictureView.isHidden = false
                
            }else{
                // 没有配图
                snp.makeConstraints({ (make) in
                    originalViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(10).constraint
                })
                pictureView.isHidden = true
            }
            
            // 时间
            timeLabel.text = statusViewModel?.createdAtText
            // 来源
            sourceLabel.text = statusViewModel?.sourceText
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 设置视图
    private func setupUI(){
        backgroundColor = UIColor.white
        // 添加控件
        addSubview(headImageView)
        addSubview(nameLabel)
        addSubview(mbrankImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(verifiedImageView)
        addSubview(contentLabel)
        addSubview(pictureView)
        // 添加约束
        headImageView.snp.makeConstraints { (make) in
            make.left.top.equalTo(10)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        
        nameLabel.snp.makeConstraints { (make) in
            make.top.equalTo(headImageView)
            make.left.equalTo(headImageView.snp.right).offset(10)
        }
        
        mbrankImageView.snp.makeConstraints { (make) in
            make.left.equalTo(nameLabel.snp.right).offset(10)
            make.centerY.equalTo(nameLabel)
        }
        
        timeLabel.snp.makeConstraints { (make) in
            make.left.equalTo(nameLabel)
            make.bottom.equalTo(headImageView)
        }
        
        sourceLabel.snp.makeConstraints { (make) in
            make.bottom.equalTo(timeLabel)
            make.left.equalTo(timeLabel.snp.right).offset(10)
        }
        
        verifiedImageView.snp.makeConstraints { (make) in
            make.centerX.equalTo(headImageView.snp.right)
            make.centerY.equalTo(headImageView.snp.bottom)
        }
        
        contentLabel.snp.makeConstraints { (make) in
            make.left.equalTo(headImageView)
            make.top.equalTo(headImageView.snp.bottom).offset(10)
        }
        
        pictureView.snp.makeConstraints { (make) in
            make.left.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp.bottom).offset(10)
            // 在内部设置
//            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        
        snp.makeConstraints { (make) in
            originalViewBottomConstraint = make.bottom.equalTo(pictureView).offset(10).constraint
        }
        
        
        // 监听高亮文字点击 后触发事件
        contentLabel.highlightTapAction = { (containerView, text, range, rect) -> () in
            print(containerView, text, range, rect)
            
            // push一个新控制器
            self.closure?()
        }
//        // 4. 接受事件回调
//        label.highlightTapAction = ^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) {
//            NSLog(@"tap text range:...");
//        };
    }

    
    // 懒加载控件
    // 头像
    private lazy var headImageView: UIImageView = UIImageView(named: "avatar_default_big")
    // 昵称
    private lazy var nameLabel: UILabel = UILabel(text: "昵称", textColor: UIColor.black, fontSize: HMNormalFontSize)
    // 会员等级
    private lazy var mbrankImageView: UIImageView = UIImageView(named: "common_icon_membership")
    // 时间
    private lazy var timeLabel: UILabel = UILabel(text: "刚刚", textColor: HMThemeColor, fontSize: HMSmallFontSize)
    // 来源
    private lazy var sourceLabel: UILabel = UILabel(text: "来自 黑马25期微博", textColor: HMThemeColor, fontSize: HMSmallFontSize)
    // 认证
    private lazy var verifiedImageView: UIImageView = UIImageView(named: "avatar_vgirl")
    // 微博正文
    private lazy var contentLabel: YYLabel = {
        // 由于中文、英文、Emoji 等字体高度不一致，或者富文本中出现了不同字号的字体，
        // 可能会造成每行文字的高度不一致。这里可以添加一个修改器来实现固定行高，或者自定义文本行位置。
        let modifier = YYTextLinePositionSimpleModifier()
        modifier.fixedLineHeight = 24
        let lab = YYLabel()
        lab.linePositionModifier = modifier
        lab.textColor = UIColor.black
//        lab.font = UIFont.systemFont(ofSize: HMNormalFontSize)
        lab.preferredMaxLayoutWidth = HMSCREENW - 20
        lab.numberOfLines = 0
        return lab
    }()
    // 配图
    private lazy var pictureView: HMHomePictureView = {
        let view = HMHomePictureView()
        view.backgroundColor = self.backgroundColor
        return view
    }()
}
